package com.example.shushiyunsst.fragment.main.home.child.miner

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.album.view.grid_view.XGridLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner.bean.MinerBean
import com.example.shushiyunsst.fragment.main.home.child.miner.bean.MinerListHeaderBean
import com.example.shushiyunsst.fragment.main.home.child.miner.sub.*
import com.example.shushiyunsst.util.MINER
import com.senon.modularapp.util.JssSpannableString
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/**
 * 矿石
 * */
@Route(path = MINER)
class MinerFragment : SimpleListFragment<MinerBean>() {

    companion object {
        fun newInstance(): MinerFragment {
            val args = Bundle()

            val fragment = MinerFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isEnableLoadMore = false
        isAddDividerItemDecoration = false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.drawable.bg_theme)

        mHeader.removeAllViews()
        addHeader(R.layout.comment_header_layout)

        val mToolbar: Toolbar = mHeader.findViewById(R.id.mToolbar)
        mToolbar.title = _mActivity.getString(R.string.minering)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        addListHeader()

    }

    private fun addListHeader() {
        val inflate = View.inflate(_mActivity, R.layout.miner_list_header_fragment, null)
        mAdapter.setHeaderView(inflate)

        val mXGridLayout: XGridLayout = inflate.findViewById(R.id.mXGridLayout)

        val mXGridAdapter= object
            :CommAdapter<MinerListHeaderBean>(
            _mActivity, R.layout.miner_list_header_item_fragment)
        {
            override fun convert(
                holder: XQuickViewHolder?,
                item: MinerListHeaderBean?,
                position: Int
            ) {

                holder?.setText(R.id.title_template,item?.title?:0)
                    ?.setImageResource(R.id.icon,item?.icon?:0)
                    ?.setOnClickListener(R.id.funView) { handListHeaderGridView(item) }
            }

        }

        mXGridLayout.setAdapter(mXGridAdapter,lifecycle)

        mXGridAdapter.addAll(minerIconArrays().asList())

        mAdapter.addChildClickViewIds(
            R.id.buying,
            R.id.miner_make,
            R.id.plant_tree,
            R.id.miner_check,
        )

        mAdapter.setOnItemChildClickListener { _, view, position ->
            handItemChildClickListener(view,position)
        }
    }

    private fun handItemChildClickListener(view: View, position: Int) {
        when(view.id){
            R.id.buying->{
                start(StartTransferFragment.newInstance())
            }
            R.id.miner_make->{
                start(MakeTreeFragment.newInstance())
            }
            R.id.miner_check->{

            }
            R.id.plant_tree->{
                start(PlantTreeFragment.newInstance())
            }
        }
    }

    private fun handListHeaderGridView(item: MinerListHeaderBean?) {
        when(item?.flag){
            START_TRANSTFER->{
                //星树交易
                start(StartTransferFragment.newInstance())
            }
            PLANT_RESULTS->{
                //栽种成就
                start(PlantResultsFragment.newInstance())
            }
            MAKE_TREE->{
                //制作星树
                start(MakeTreeFragment.newInstance())
            }
            MINER_NOTIFY->{
                //矿石通知
                start(MinerNotifyFragment.newInstance())
            }
            MINER_POOL->{
                //矿池
                start(MinerPoolFragment.newInstance())
            }
            MINER_STRUTS->{
                //矿石攻略
                start(MinerStrutsFragment.newInstance())
            }
        }
    }

    override fun getItemLayout() = R.layout.miner_fragment

    override fun convertItem(helper: JssBaseViewHolder?, item: MinerBean?) {

        val str0 = "(3000U/棵)"

        val str1 = "V-Ⅰ型能量星树\t$str0"

        helper?.setText(
            R.id.titleTv,
            JssSpannableString(_mActivity, str1)
                .first(str0).size(9)
        )
            ?.setText(R.id.platform_type_one, "平台型")
            ?.setText(R.id.platform_type_one_struts, "链上恒定发行999棵")
            ?.setText(R.id.platform_type_two, "合约型")
            ?.setText(R.id.platform_type_two_struts, "已产出st合成 5倍矿石产出")
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(MinerBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.temp_fack_data)
        parseDate(jsonFromRaw)
    }
}